package com.beecloud.microservice.user.commom;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public class RichList {
    public static <T> Optional<T> head(List<T> target) {
        return  safeElement(target, 0);
    }
    public static <T> Optional<T> safeElement(List<T> target, int index) {
        return Optional.of(target).filter(p -> p.size() > index).map(p->p.get(index));
    }

    public static <T, U> List<U> convertList(List<T> original, Function<T, U> mapper) {
        return original.stream().map(mapper).collect(Collectors.toList());
    }

}
